package com.dces.jx.billtest.api.xwManageHead;
import com.dces.jx.billtest.common.service.ExcelService;
import com.dces.jx.billtest.common.model.PCodeType;
import com.dces.jx.billtest.dto.exgCurrManage.DcBillDeleteLogParam;
import com.dces.jx.billtest.dto.xwManageHead.*;
import com.dces.jx.billtest.xwManageHead.service.DcBillHeadXwtService;
import com.xdo.pcode.service.PCodeHolder;
import com.xdo.domain.PageParam;
import com.xdo.domain.ResultObject;
import com.xdo.common.token.UserInfoToken;
import com.xdo.springboot.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
/**
 * generated by Generate dc
 * 
 *
 * @author xdoV2.0
 * @date: 2021-9-7
 */
@RestController
@RequestMapping("v1/dcBillHeadXwt")
@Api(tags = "接口")
public class DcBillHeadXwtController extends BaseController {
    @Resource
    private DcBillHeadXwtService dcBillHeadXwtService;
    @Resource
    private PCodeHolder pCodeHolder;
    @Resource
    private ExcelService excelService;

    //TODO controller逻辑移植service中，保持controller整洁

    @ApiOperation("从备份表里恢复数据")
    @PostMapping("insertHeadList")
    public ResultObject<Object> insertHeadList(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam){
        int n=dcBillHeadXwtService.insertHeadList(dcBillHeadXwtParam);
        if (n>0){
            return ResultObject.createInstance(true,"恢复成功");
        }else {
            return ResultObject.createInstance(false,"恢复失败");
        }
    }
    @ApiOperation("删除表头数据,根据billListNo")
    @PostMapping("delHeadListByBillListNo")
    public ResultObject<Object> delHeadListByBillListNo(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam){
        int n=dcBillHeadXwtService.delHeadListByBillListNo(dcBillHeadXwtParam);
        if (n>0){
            return ResultObject.createInstance(true,"删除成功");
        }else {
            return ResultObject.createInstance(false,"删除失败");
        }
    }
    @ApiOperation("获取当前表头的表体数据的条数")
    @PostMapping("getListCount")
    public ResultObject<Object> getListCount(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam){
        int n=dcBillHeadXwtService.getListCount(dcBillHeadXwtParam);
        if (n>0){
            return ResultObject.createInstance(true);
        }else {
            return ResultObject.createInstance(false);
        }
    }
    @ApiOperation("清空表体数据")
    @PostMapping("deleteList")
    public ResultObject<Object> deleteList(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam){
        int n=dcBillHeadXwtService.deleteList(dcBillHeadXwtParam);
        return ResultObject.createInstance(true,"清空成功",n);
    }
    @ApiOperation("删改申请:恢复")
    @PostMapping("recoveryData")
    public ResultObject<Object> recoveryData(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam){
        int n=dcBillHeadXwtService.recoveryData(dcBillHeadXwtParam);
        if (n>0){
            return ResultObject.createInstance(true,"恢复成功",n);
        }
        return ResultObject.createInstance(false,"失败");
    }
    @ApiOperation("删改恢复判断表头数据是否备份")
    @PostMapping("getHeadHisList")
    public ResultObject<Object> getHeadHisList(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam){
        int n=dcBillHeadXwtService.getHeadHis(dcBillHeadXwtParam);
        if (n==0){
            return ResultObject.createInstance(true,"未备份",n);
        } else {
            return ResultObject.createInstance(true,"已备份",n);
        }
    }

    @ApiOperation("修改状态和清单申报信息")
    @PostMapping("updList")
    public ResultObject<Object> updList(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam,UserInfoToken userInfo){

            int n=dcBillHeadXwtService.updList(dcBillHeadXwtParam,userInfo);
            if (n>0){
                return ResultObject.createInstance(true,"成功",n);
            }
            return ResultObject.createInstance(true,"失败",n);
    }
    @ApiOperation("删改申请判断是否备份")
    @PostMapping("getHeadHis")
    public ResultObject<Object> getHeadHis(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam){
        int n=dcBillHeadXwtService.getHeadHis(dcBillHeadXwtParam);
        if (n==0){
            //表头备份
            int headN=dcBillHeadXwtService.insertHeadHis(dcBillHeadXwtParam);
            //表体备份
            int listN=dcBillHeadXwtService.insertListHis(dcBillHeadXwtParam);
            return ResultObject.createInstance(true,"正在备份");
        } else {
            return ResultObject.createInstance(true,"已备份",n);
        }
    }
    @ApiOperation("表头删除后添加")
    @PostMapping("insertDcBillDeleteLog")
    public ResultObject<Object> insertDcBillDeleteLog(@Valid @RequestBody DcBillDeleteLogParam dcBillDeleteLogParam){
        int n=dcBillHeadXwtService.insertDcBillDeleteLog(dcBillDeleteLogParam);
        if (n>0){
            return ResultObject.createInstance(true,"添加成功");
        }
        return ResultObject.createInstance(false,"添加失败");
    }
    @ApiOperation("根据当前账册号查询经营单位,加工单位,录入单位的编码,名称和社会信用代码")
    @PostMapping("getCodeNameCreditCodeByEmsNo")
    public ResultObject<EmsCusHeadCurDto> getCodeNameCreditCodeByEmsNo(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam){
            EmsCusHeadCurDto dc=dcBillHeadXwtService.getCodeNameCreditCodeByEmsNo(dcBillHeadXwtParam);
        return ResultObject.createInstance(true,"查询成功",dc);
    }
    @ApiOperation("查询报关单申报单位名称和社会信用代码")
    @PostMapping("getAgentNameAndCreditCode")
    public ResultObject<DcBillHeadXwtDto> getAgentNameAndCreditCode(@Valid @RequestBody DcBillHeadXwtDto dcBillHeadXwtDto){
        DcBillHeadXwtDto dc=dcBillHeadXwtService.getAgentNameAndCreditCode(dcBillHeadXwtDto);
        return ResultObject.createInstance(true,"查询成功",dc);
    }
    @ApiOperation("查询归并类型接口")
    @PostMapping("getMergeType")
    public ResultObject<List<DcBillCodeItemDto>> getMergeType(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getMergeType();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    @ApiOperation("查询报流转类型接口")
    @PostMapping("getRotateType")
    public ResultObject<List<DcBillCodeItemDto>> getRotateType(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getRotateType();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    @ApiOperation("查询报关单类型接口")
    @PostMapping("getEntryType")
    public ResultObject<List<DcBillCodeItemDto>> getEntryType(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getEntryType();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    @ApiOperation("查询报关类型接口")
    @PostMapping("getDclcusType")
    public ResultObject<List<DcBillCodeItemDto>> getDclcusType(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getDclcusType();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    @ApiOperation("查询报关标志接口")
    @PostMapping("getDclcusMark")
    public ResultObject<List<DcBillCodeItemDto>> getDclcusMark(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getDclcusMark();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    @ApiOperation("查询料件成品标志接口")
    @PostMapping("getGMark")
    public ResultObject<List<DcBillCodeItemDto>> getGMark(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getGMark();
        return ResultObject.createInstance(true,"查询成功",list);
    }

    @ApiOperation("查询查询清单类型接口")
    @PostMapping("getListType")
    public ResultObject<List<DcBillCodeItemDto>> getListType(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getListType();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    @ApiOperation("查询查询清单状态接口")
    @PostMapping("status")
    public ResultObject<List<DcBillCodeItemDto>> getStatus(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getStatus();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    @ApiOperation("查询查询核扣标志信息接口")
    @PostMapping("getVrfdedMark")
    public ResultObject<List<DcBillCodeItemDto>> getVrfdedMark(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getVrfdedMark();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    @ApiOperation("查询查询报关状态接口")
    @PostMapping("entryStatus")
    public ResultObject<List<DcBillCodeItemDto>> getEntryStatus(){
        List<DcBillCodeItemDto> list=dcBillHeadXwtService.getEntryStatus();
        return ResultObject.createInstance(true,"查询成功",list);
    }
    /**
     * @param dcBillHeadXwtParam
     * @param pageParam
     * @param userInfo
     * @return
     */
    @ApiOperation("分页查询接口")
    @PostMapping("list")
    public ResultObject<List<DcBillHeadXwtDto>> getListPaged(@RequestBody DcBillHeadXwtParam dcBillHeadXwtParam, PageParam pageParam, UserInfoToken userInfo) {
        ResultObject<List<DcBillHeadXwtDto>> paged = dcBillHeadXwtService.getListPaged(dcBillHeadXwtParam, pageParam);
        return paged;
    }
    /**
     * @param dcBillHeadXwtParam
     * @param userInfo
     * @return
     */
    @ApiOperation("新增接口")
    @PostMapping()
    public ResultObject<DcBillHeadXwtDto> insert(@Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam, UserInfoToken userInfo) {
        //TODO 变量命名规则，不要写n、n2，行尾注释提到行头
        int n=dcBillHeadXwtService.getCount(dcBillHeadXwtParam);
        int n2=dcBillHeadXwtService.getEmsListNoCount(dcBillHeadXwtParam);
        if (n==0 && n2==0){
            long s=dcBillHeadXwtService.getSBillListNoXwtDto();
            dcBillHeadXwtParam.setBillListNo("Z"+s);//设置清单流水号默认值

            ResultObject<DcBillHeadXwtDto> resultObject = ResultObject.createInstance(true, ResultObject.INSERT_SUCCESS);
            DcBillHeadXwtDto dcBillHeadXwtDto = dcBillHeadXwtService.insert(dcBillHeadXwtParam, userInfo);
            if (dcBillHeadXwtDto != null) {
                resultObject.setData(dcBillHeadXwtDto);
            } else {
                resultObject.setSuccess(false);
                resultObject.setMessage(ResultObject.INSERT_FAIL);
            }
            return resultObject;
        }else {
            return ResultObject.createInstance(false,"同一手(帐)册号下不允许重复!");
        }


    }
    /**
     * @param sid
     * @param dcBillHeadXwtParam
     * @param userInfo
     * @return
     */
    @ApiOperation("修改接口")
    @PutMapping("{sid}")
    public ResultObject<DcBillHeadXwtDto> update(@PathVariable String sid, @Valid @RequestBody DcBillHeadXwtParam dcBillHeadXwtParam, UserInfoToken userInfo) {
        dcBillHeadXwtParam.setSid(sid);
        DcBillHeadXwtDto dcBillHeadXwtDto = dcBillHeadXwtService.update(dcBillHeadXwtParam, userInfo);
        ResultObject resultObject = ResultObject.createInstance(true, ResultObject.UPDATE_SUCCESS);
        if (dcBillHeadXwtDto != null) {
            resultObject.setData(dcBillHeadXwtDto);
        } else {
            resultObject.setSuccess(false);
            resultObject.setMessage(ResultObject.UPDATE_FAIL);
        }
        return resultObject;
    }
    // TODO patch api
    /**
     * @param sids
     * @return
     */
    @ApiOperation("删除接口")
    @DeleteMapping("{sids}")
    public ResultObject delete(@PathVariable List<String> sids, UserInfoToken userInfo) {
        ResultObject resultObject = ResultObject.createInstance(true, ResultObject.DELETE_SUCCESS);
        // 检查是否存在表体数据
		 dcBillHeadXwtService.delete(sids, userInfo);
        return resultObject;
    }
    /**
     * @param exportParam
     * @param userInfo
     * @return
     * @throws Exception
     */
    @ApiOperation("Excel数据导出接口")
    @PostMapping(value = "/export")
    public ResponseEntity export(@Valid @RequestBody DcBillHeadXwtExportParam exportParam, UserInfoToken userInfo) throws Exception{
        List<DcBillHeadXwtDto> dcBillHeadXwtDtos = dcBillHeadXwtService.selectAll(exportParam.getExportColumns(), userInfo);
        convertForPrint(dcBillHeadXwtDtos);
        return excelService.getExcelHeaders(URLEncoder.encode(exportParam.getName(), "utf-8"), exportParam.getHeader(), dcBillHeadXwtDtos, userInfo);
    }
    /**
     * 导出，pCode转换中文名称
     * @param list
     * @return
     */
    public void convertForPrint(List<DcBillHeadXwtDto> list) {
        for(DcBillHeadXwtDto item : list) {
        }
    }
}
